<?php

/**
 * 量子统计
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . 'languages/' .$_CFG['lang']. '/admin/statistic.php');
$smarty->assign('lang', $_LANG);

/* act操作项的初始化 */
if (empty($_REQUEST['act']))
{
    $_REQUEST['act'] = 'view';
}
else
{
    $_REQUEST['act'] = trim($_REQUEST['act']);
}

if ($_REQUEST['act'] == 'view')
{
	//默认是当天时间
	$date2 = date('Y-m-d',time());
	$date1 = date("Y-m-d",strtotime('-7 day',strtotime($date2)));

	//选择时间
	if(isset($_POST['date1']) && isset($_POST['date2']))
	{
		$date1 = $_POST['date1'];
		$date2 = $_POST['date2'];
	}

    $q = new ConcreteFacory($date1, $date2);
	$day = $q->transferToArray();

	assign_query_info();
	$smarty->assign('ur_here',      $_LANG['quantum_stats']);
	$smarty->assign('date1',      $date1);
	$smarty->assign('date2',      $date2);
	$smarty->assign('day', $day);
	$smarty->display('quantum_stats.htm');
	
	
}
/**
 * 量子统计
 */
class Quantum 
{
	var $start_time = 0;
	var $end_time = 0;
	var $date = 0;
	var $pv = 0;                //访客数
	var $uv = 0;                //浏览量
	var $all_order = 0;
	var $all_amount = 0;
	var $turnover = 0;          //转化率
	var $per_sum = 0;           //客单价
	var $ali_order = 0;
	var $ali_amount = 0;
	var $cash_order = 0;
	var $cash_amount = 0;


	function __construct($start_time, $end_time)
	{	
		$this->start_time = $start_time;
		if($end_time <= time()){
			$this->end_time = $end_time;
		}else{
			$this->end_time = time();
		}
		$this->initAll();
	}

	/* 访客数(IP)、浏览量 */
	public function getPvUv()
	{
    	$sql = "SELECT count( DISTINCT ip_address ) AS pv, SUM(visit_times) AS uv".
                " FROM " .$GLOBALS['ecs']->table('stats').
                " WHERE access_time >= '$this->start_time' AND access_time <= $this->end_time";
    	$res = $GLOBALS['db']->GetRow($sql);

    	$this->pv = $res['pv'];
    	$this->uv = $res['uv']; 

	}


	/* 获得支付宝：订单数、总金额等 */
	public function getAli()
	{
		$pay_id = '1';    //支付宝
		$res = $this->getOrderAmount($pay_id);
		$this->ali_order = $res['orders'];        //订单数
		$this->ali_amount = $res['amount'];     //支付宝支付总金额
	}

	/* 获得货到付款：总订单数、总金额等 */
	public function getCash()
	{
		$pay_id = '3';    //货到付款
		$res = $this->getOrderAmount($pay_id);
		$this->cash_order = $res['orders'];        
		$this->cash_amount = $res['amount'];     
	}

	/* 获得：总订单数、总金额等 */
	public function getAll()   
	{
		$this->all_order = $this->ali_order + $this->cash_order;        //订单数
		$this->all_amount = $this->ali_amount + $this->cash_amount;        //总金额
		if($this->pv != 0){
			$this->turnover = sprintf('%.5f', $this->all_order/$this->pv) * 100;         //成交转化率
		}
		if($this->all_order != 0){
			$this->per_sum = sprintf('%.3f', $this->all_amount/$this->all_order);      //客单价（每个顾客平均购买金额）
		}
	}
	
	/* 订单量、总金额 */
	private function getOrderAmount($pay_id='')
	{
		$sql = "SELECT COUNT(*) as orders, sum(goods_amount) + sum(shipping_fee) as amount".
					" FROM " .$GLOBALS['ecs']->table('order_info').                         
					" WHERE order_status NOT IN(2,3) ".
					" AND add_time >= '$this->start_time' AND add_time <= $this->end_time".
					" AND pay_id  = {$pay_id}";
	    if($pay_id === '1'){  //支付宝支付，要支付了
	    	$sql = $sql . " AND pay_status ='2' ";
	    }

		$res = $GLOBALS['db']->GetRow($sql);
		if( $res['orders'] == 0){
			$res['amount'] = 0;
		}
		return $res;
	}

	/* 初始化所有数据 */
	public function initAll()
	{
		$this->getPvUv();
		$this->getAli();
		$this->getCash();
		$this->getAll();
	}
}

/**
* 工厂模式，生产quantum
*/
class ConcreteFacory
{
	private   $time = array();
	public    $quantum = array();

	public function __construct($date1, $date2)
	{
		$this->initDate($date1, $date2);
		$this->createQuantum();
	}

	private function initDate($date1, $date2)
	{
   		$i=0; 
		while ( strtotime($date2) >= strtotime("+".$i." day",strtotime($date1) ) ) {
			$date = strtotime( date("Y-m-d",strtotime("+".$i++." day",strtotime($date1))) ); 
			$this->time[$i]['start_time'] = mktime(0, 0, 0, date('m',$date), date('d',$date), date('Y',$date));
			$this->time[$i]['end_time'] = mktime(0, 0, 0, date('m', $date), date('d', $date)+1, date('Y', $date))-1;
		}
	}

	public function createQuantum( )
	{
		foreach ($this->time as $key => $value) {
			$this->quantum[$key] = new Quantum($value['start_time'], $value['end_time']);
		}
	}

	public function transferToArray()
	{
		$len = count($this->quantum);
		foreach ($this->quantum as $key => $value) {
			//日期倒序输出，将对象转换为数组
			$this->quantum[$len] = (array) $value;
			$this->quantum[$len]['date'] = date('Y-m-d', $value->start_time);
			$len--;
		}
		return $this->quantum;
	}
}


?>